package cn.tedu.ivos2.application.service.impl;

import cn.tedu.ivos2.application.mapper.ApplicationMapper;
import cn.tedu.ivos2.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos2.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos2.application.service.ApplicationService;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Service
public class ApplicationServiceImpl implements ApplicationService {

    private final ApplicationMapper applicationMapper;
    private final AuditService auditService;
    private final UserMapper userMapper;

    @Autowired
    public ApplicationServiceImpl(ApplicationMapper applicationMapper, AuditService auditService, UserMapper userMapper) {
        this.applicationMapper = applicationMapper;
        this.auditService = auditService;
        this.userMapper = userMapper;
    }

    public List<ApplicationVO> query(ApplicationQuery query) {
        List<ApplicationVO> list = applicationMapper.query(query);
        list.forEach(this::assignAuditUserList);
        return list;
    }

    public void add(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setCreateTime(new Date());
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        applicationMapper.add(application);
        auditService.addAudit(application);
    }

    public void cancel(Long id) {
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        auditService.deleteAuditByApplicationId(id);
    }

    private void assignAuditUserList(ApplicationVO application) {
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList = auditService.queryAuditByApplicationId(application.getId());
        auditVOList.forEach(auditVO -> {
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        });
        StringJoiner sj = new StringJoiner(",");
        auditUsernameList.forEach(sj::add);
        application.setAuditUserIdList(auditUserIdList);
        application.setAuditUsernameList(sj.toString());
    }
}
